"---------------------------------
"基础配置
"Basic configuration
"---------------------------------
"
"将Esc改为Ctrl+j
imap <C-J> <ESC>
"将:改为空格
nmap <space> :
"将NERDTree设置快捷键
map <silent> <S-t> :NERDTreeToggle<CR>
"默认编码
set encoding=utf8
"默认字体
set guifont=Hack_Nerd_Font
"默认显示行号
set nu
"能使用backspace回删
set backspace=2
"语法检测
syntax on
"设置c语言自动对齐
set cindent
"设置可以在neovim使用鼠标
set mouse=a
"设置历史记录条数
set history=500

"--------------------------------
"插件配置
"Basic configuration
"--------------------------------

call plug#begin('~/.config/nvim/plugged')
"目录树
"Directory tree
Plug 'scrooloose/nerdtree'
"标签栏列表,源码查看
"Tag bar list (for source code view
Plug 'vim-scripts/taglist.vim'
"多语言代码纠错
"Multi -language code correction
Plug 'dense-analysis/ale'
"彩虹括号
"Rainbow bracket
Plug 'luochen1990/rainbow'
"括号自动补全
"Auto-complete brackets
Plug 'Raimondi/delimitMate'
"C/C++自动格式化
"Code automatic formatting
Plug 'Chiel92/vim-autoformat'
"代码高亮
"Code highlight
Plug 'octol/vim-cpp-enhanced-highlight'
"C/C++代码自动补全
"C/C ++ code automatic completion
Plug 'neoclide/coc.nvim', {'branch': 'release'}
"NERDTreeToggle美化
"NERDTREE beautification
Plug 'ryanoasis/vim-devicons'
"wakatime
Plug 'wakatime/vim-wakatime'
"将剪切板中的图片插入为Markdown代码
"Insert the picture in the shear plate into the Markdown documentation
Plug 'ferrine/md-img-paste.vim'
"Markdowm语法高亮插件
Plug 'godlygeek/tabular' "必要插件，安装在vim-markdown前面
Plug 'plasticboy/vim-markdown'
"Markdown文本下自动在当前光标前生成目录
Plug 'mzlogin/vim-markdown-toc'
"借助浏览器浏览Markdown文件,如果已经安装了nodejs及yarn
" Preview the Markdown document in real time with the browser,If nodejs and yarn have been installed
Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install' }
"nerdtree显示git状态
"NERDTree Show the git status
Plug 'xuyuanp/nerdtree-git-plugin'

call plug#end()

"-------------------------------
"插件详细配置
"Plug -in detailed configuration
"-------------------------------


"'dense-analysis/ale' “多语言代码差错”插件-ALE 配置
"
"始终开启标志列
let g:ale_sign_column_always = 1
let g:ale_set_highlights = 0
"自定义error和warning图标
let g:ale_sign_error = '✗'
let g:ale_sign_warning = '⚡'
"在vim自带的状态栏中整合ale
let g:ale_statusline_format = ['✗', '⚡', '✔ ']
"显示Linter名称,出错或警告等相关信息
let g:ale_echo_msg_error_str = 'E'
let g:ale_echo_msg_warning_str = 'W'
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
"普通模式下，sp前往上一个错误或警告，sn前往下一个错误或警告
nmap sp <Plug>(ale_previous_wrap)
nmap sn <Plug>(ale_next_wrap)
"<Leader>s触发/关闭语法检查
nmap <Leader>s :ALEToggle<CR>
"<Leader>d查看错误或警告的详细信息
nmap <Leader>d :ALEDetail<CR>
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}\ %{ALEGetStatusLine()}
"cpp NOT C++
let g:ale_linters = {
			\   'cpp': ['gcc'],
			\   'c': ['gcc'],
			\   'python': ['pylint'],
			\}


"Raimondi/delimitMate' “括号补全”插件-delimiMate配置
"
"Bundle 'Raimondi/delimitMate'


"'octol/vim-cpp-enhanced-highlight' “代码高亮”插件-highlight配置
"
"高亮类，成员函数，标准库和模板
let g:cpp_class_scope_highlight = 1
let g:cpp_member_variable_highlight = 1
let g:cpp_concepts_highlight = 1
let g:cpp_experimental_simple_template_highlight = 1
"文件较大时使用下面的设置高亮模板速度较快，但会有一些小错误
let g:cpp_experimental_template_highlight = 1


"'neoclide/coc.nvim' “C/C++自动格式化”插件-coc配置
"
noremap <C-G> :Autoformat<CR>
let g:autoformat_verbosemode=1
"保存文件时自动格式化代码
autocmd FileType c,cpp autocmd BufWritePre * :Autoformat
"设置自动格式化风格google
let g:formatdef_clangformat_google = '"clang-format -style google -"'
let g:formatters_c = ['clangformat_google']
autocmd BufWrite *.sql,*.c,*.py,*.java,*.js :Autoformat "设置发生保存事件时执行格式化


"coc.nvim代码自动补全配置
"
" Set internal encoding of vim, not needed on neovim, since coc.nvim using some
" unicode characters in the file autoload/float.vim
set encoding=utf-8

" TextEdit might fail if hidden is not set.
set hidden

" Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup

" Give more space for displaying messages.
set cmdheight=2

" Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable
" delays and poor user experience.
set updatetime=300

" Don't pass messages to |ins-completion-menu|.
set shortmess+=c

" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved.
if has("nvim-0.5.0") || has("patch-8.1.1564")
	" Recently vim can merge signcolumn and number column into one
	set signcolumn=number
else
	set signcolumn=yes
endif

" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
			\ pumvisible() ? "\<C-n>" :
			\ CheckBackspace() ? "\<TAB>" :
			\ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! CheckBackspace() abort
	let col = col('.') - 1
	return !col || getline('.')[col - 1]  =~# '\s'
endfunction

" Use <c-space> to trigger completion.
if has('nvim')
	inoremap <silent><expr> <c-space> coc#refresh()
else
	inoremap <silent><expr> <c-@> coc#refresh()
endif

" Make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
			\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

" GoTo code navigation.
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Use K to show documentation in preview window.
nnoremap <silent> K :call ShowDocumentation()<CR>

function! ShowDocumentation()
	if CocAction('hasProvider', 'hover')
		call CocActionAsync('doHover')
	else
		call feedkeys('K', 'in')
	endif
endfunction

" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)

" Formatting selected code.
xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)

augroup mygroup
	autocmd!
	" Setup formatexpr specified filetype(s).
	autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
	" Update signature help on jump placeholder.
	autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

" Applying codeAction to the selected region.
" Example: `<leader>aap` for current paragraph
xmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>a  <Plug>(coc-codeaction-selected)

" Remap keys for applying codeAction to the current buffer.
nmap <leader>ac  <Plug>(coc-codeaction)
" Apply AutoFix to problem on the current line.
nmap <leader>qf  <Plug>(coc-fix-current)

" Run the Code Lens action on the current line.
nmap <leader>cl  <Plug>(coc-codelens-action)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

" Remap <C-f> and <C-b> for scroll float windows/popups.
if has('nvim-0.4.0') || has('patch-8.2.0750')
	nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
	nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
	inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
	inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
	vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
	vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif

" Use CTRL-S for selections ranges.
" Requires 'textDocument/selectionRange' support of language server.
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)

" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocActionAsync('format')

" Add `:Fold` command to fold current buffer.
command! -nargs=? Fold :call     CocAction('fold', <f-args>)

" Add `:OR` command for organize imports of the current buffer.
command! -nargs=0 OR   :call     CocActionAsync('runCommand', 'editor.action.organizeImport')

" Add (Neo)Vim's native statusline support.
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline.
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

" Mappings for CoCList
" Show all diagnostics.
nnoremap <silent><nowait> <space>a  :<C-u>CocList diagnostics<cr>
" Manage extensions.
nnoremap <silent><nowait> <space>e  :<C-u>CocList extensions<cr>
" Show commands.
nnoremap <silent><nowait> <space>c  :<C-u>CocList commands<cr>
" Find symbol of current document.
nnoremap <silent><nowait> <space>o  :<C-u>CocList outline<cr>
" Search workspace symbols.
nnoremap <silent><nowait> <space>s  :<C-u>CocList -I symbols<cr>
" Do default action for next item.
nnoremap <silent><nowait> <space>j  :<C-u>CocNext<CR>
" Do default action for previous item.
nnoremap <silent><nowait> <space>k  :<C-u>CocPrev<CR>
" Resume latest coc list.
nnoremap <silent><nowait> <space>p  :<C-u>CocListResume<CR>


"'luochen1990/rainbow' “彩虹括号”插件-rainbow配置
"
"自动启动插件，如果使用命令:RainbowToggle启动插件，则设为0
let g:rainbow_active = 1
"配置颜色，及是否高亮括号对
"‘guifgs’：GUI界面的括号颜色(将按顺序循环使用)
"‘ctermfgs’：终端下的括号颜色(同上,插件将根据环境进行选择)
"‘operators’：与同级括号一起高亮的运算符('_,\|+\|-_' ,、 +、 -)
"‘parentheses’：描述哪些模式将被当作括号处理,每一组括号由两个vim正则表达式描述
"‘separately’: 针对文件类型(由&filettype决定)作不同的配置,未被单独设置的文件类型使用*下的配置,值为0表示仅对该类型禁用插件
let g:rainbow_conf ={}


"errine/md-img-paste.vim “将剪切板中的图片插入为Markdown "代码”插件-md-img-paste配置
"
"设置默认储存文件夹。这里表示储存在当前文档所在文件夹下的'pic'文件夹下，相当于 ./picture/
let g:mdip_imgdir = './Md_picture'
"设置默认图片名称。当图片名称没有给出时，使用默认图片名称,例如：如果不输入文件名直接回车，将保存为 <前缀>_日期-时间.png 名称的文件。
let g:mdip_imgname = 'image'
"设置快捷键，个人喜欢 Shift+p 的方式，比较直观
autocmd FileType markdown nnoremap <silent> <S-i> :call mdip#MarkdownClipboardImage()<CR>
"可以看见，在快捷键设置这里我在末尾加了 F%i。因为这个插件初始设置插入的格式为标准Markdown格式，即 ![](pic/image1.png)。


"'plasticboy/vim-markdown' “Markdown语法高亮”插件-markdown配置
"
"由于我们还会使用到Latex数学公式，可以设置：
let g:vim_markdown_math = 1
""查看所有配置建议
":help vim-markdwon
"[[ "跳转上一个标题
"]] "跳转下一个标题
"]c "跳转到当前标题
"]u "跳转到副标题
"zr "打开下一级折叠
"zR "打开所有折叠
"zm "折叠当前段落
"zM "折叠所有段落
":Toc "显示目录


"Plug 'mzlogin/vim-markdown-toc' “自动在光标前生成目录”插件-toc配置
"
"在当前光标后生成目录
":GenTocMarked
"更新目录
":UpdateToc
"储存时自动更新目录
let g:vmt_auto_update_on_save = 1
"由于自动生成的目录层级太多，因此我在配置文件中写了一个方程，自动找到较低的目录层级，然后删除之：
function RToc()
	exe "/-toc .* -->"
	let lstart=line('.')
	exe "/-toc -->"
	let lnum=line('.')
	execute lstart.",".lnum."g/           /d"
endfunction


"Plug 'iamcco/markdown-preview.nvim' “通过浏览器实时浏览Markdown文件”插件-priview配置
"打开/关闭浏览
":MarkdownPreviewToggel
" 指定浏览器路径
"let g:mkdp_path_to_chrome = "/usr/bin/google-chrome-stable"
" 指定预览主题，默认Github
let g:mkdp_markdown_css=''
" set to 1, nvim will open the preview window after entering the markdown buffer
" default: 0
let g:mkdp_auto_start = 0

" set to 1, the nvim will auto close current preview window when change
" from markdown buffer to another buffer
" default: 1
let g:mkdp_auto_close = 1

" set to 1, the vim will refresh markdown when save the buffer or
" leave from insert mode, default 0 is auto refresh markdown as you edit or
" move the cursor
" default: 0
let g:mkdp_refresh_slow = 0

" set to 1, the MarkdownPreview command can be use for all files,
" by default it can be use in markdown file
" default: 0
let g:mkdp_command_for_global = 0

" set to 1, preview server available to others in your network
" by default, the server listens on localhost (127.0.0.1)
" default: 0
let g:mkdp_open_to_the_world = 0

" use custom IP to open preview page
" useful when you work in remote vim and preview on local browser
" more detail see: https://github.com/iamcco/markdown-preview.nvim/pull/9
" default empty
let g:mkdp_open_ip = ''

" specify browser to open preview page
" for path with space
" valid: `/path/with\ space/xxx`
" invalid: `/path/with\\ space/xxx`
" default: ''
let g:mkdp_browser = '/usr/bin/google-chrome-stable'

" set to 1, echo preview page url in command line when open preview page
" default is 0
let g:mkdp_echo_preview_url = 0

" a custom vim function name to open preview page
" this function will receive url as param
" default is empty
let g:mkdp_browserfunc = ''

" options for markdown render
" mkit: markdown-it options for render
" katex: katex options for math
" uml: markdown-it-plantuml options
" maid: mermaid options
" disable_sync_scroll: if disable sync scroll, default 0
" sync_scroll_type: 'middle', 'top' or 'relative', default value is 'middle'
"   middle: mean the cursor position alway show at the middle of the preview page
"   top: mean the vim top viewport alway show at the top of the preview page
"   relative: mean the cursor position alway show at the relative positon of the preview page
" hide_yaml_meta: if hide yaml metadata, default is 1
" sequence_diagrams: js-sequence-diagrams options
" content_editable: if enable content editable for preview page, default: v:false
" disable_filename: if disable filename header for preview page, default: 0
let g:mkdp_preview_options = {
			\ 'mkit': {},
			\ 'katex': {},
			\ 'uml': {},
			\ 'maid': {},
			\ 'disable_sync_scroll': 0,
			\ 'sync_scroll_type': 'middle',
			\ 'hide_yaml_meta': 1,
			\ 'sequence_diagrams': {},
			\ 'flowchart_diagrams': {},
			\ 'content_editable': v:false,
			\ 'disable_filename': 0,
			\ 'toc': {}
			\ }

" use a custom markdown style must be absolute path
" like '/Users/username/markdown.css' or expand('~/markdown.css')
let g:mkdp_markdown_css = ''

" use a custom highlight style must absolute path
" like '/Users/username/highlight.css' or expand('~/highlight.css')
let g:mkdp_highlight_css = ''

" use a custom port to start server or empty for random
let g:mkdp_port = ''

" preview page title
" ${name} will be replace with the file name
let g:mkdp_page_title = '「${name}」'

" recognized filetypes
" these filetypes will have MarkdownPreview... commands
let g:mkdp_filetypes = ['markdown']

" set default theme (dark or light)
" By default the theme is define according to the preferences of the system
let g:mkdp_theme = 'dark'

"'iamcco/markdown-preview.nvim'插件映射：
nmap <C-o> <Plug>MarkdownPreview
nmap <C-p> <Plug>MarkdownPreviewStop
nmap <C-p> <Plug>MarkdownPreviewToggle


"'scrooloose/nerdtree' “目录树”插件-nerdtree配置
"
"" 显示行号
let NERDTreeShowLineNumbers=1
let NERDTreeAutoCenter=1
" 是否显示隐藏文件
let NERDTreeShowHidden=1
" 设置宽度
let NERDTreeWinSize=43
" 在终端启动vim时，共享NERDTree
let g:nerdtree_tabs_open_on_console_startup=1
" 忽略一下文件的显示
"let NERDTreeIgnore=['\.pyc','\~$','\.swp']
" 显示书签列表
let NERDTreeShowBookmarks=1


"Plug 'xuyuanp/nerdtree-git-plugin'“显示git状态”插件-nerdtree-git配置
"
"显示git信息,modified：修改；staged：暂存；untracked：未跟踪；renamed：更名；unmerged：未合并路径；deleted：删除；dirty：脏的；clean：干净的；unknown：未知的。
let g:NERDTreeGitStatusIndicatorMapCustom = {
			\ "Modified"  : "✹",
			\ "Staged"    : "✚",
			\ "Untracked" : "✭",
			\ "Renamed"   : "➜",
			\ "Unmerged"  : "═",
			\ "Deleted"   : "✖",
			\ "Dirty"     : "✗",
			\ "Clean"     : "✔︎",
			\ 'Ignored'   : '☒',
			\ "Unknown"   : "?"
			\ }
"显示ignored状态
let g:NERDTreeGitStatusShowIgnored = 1
let g:NERDTreeGitStatusUseNerdFonts = 1


"Plug 'vim-scripts/taglist.vim' “源代码查看”插件-taglist配置
"
"打开文件类型检测功能
"filetype plugin indent on　　　　　　　　
"设定系统中ctags程序的位置
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
"不同时显示多个文件的tag，只显示当前文件的
let Tlist_Show_One_File = 1
"如果taglist窗口是最后一个窗口，则退出vim
let Tlist_Exit_OnlyWindow = 1
"按Ctrl+y等同于在命令行模式输入:TlistToggle
map <silent> <C-y> :TlistToggle<Cr>
"在右侧窗口中显示taglist窗口
let Tlist_Use_Right_Window =0
"每次启动vim自动导入该tags文件
set tags =~/Documents/C_Projects/tags
"ctags可执行文件的路径
let Tlist_Ctags_Cmd='/usr/local/bin/ctags'
"打开文件时不自动打开Taglist窗口
let Tlist_Auto_Open=0
"自动检查
syntax on
"配置窗口大小
set tabstop=4
set autoindent
set shiftwidth=4
filetype on


